/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import entities.Client;
import java.lang.reflect.Field;
import java.util.Comparator;
import org.primefaces.model.SortOrder;


public class Sort implements Comparator<Client> {

    private String sortField;
    
    private SortOrder sortOrder;
    
    public Sort(String sortField, SortOrder sortOrder) {
        this.sortField = sortField;
        this.sortOrder = sortOrder;
    }

    

    @Override
    public int compare(Client t, Client t1) {
        try {
            Field field = Client.class.getDeclaredField(this.sortField);
            field.setAccessible(true);
            Object value1 = field.get(t);
            
            Object value2 = field.get(t1);

            int value = ((Comparable)value1).compareTo(value2);
            
            return SortOrder.ASCENDING.equals(sortOrder) ? value : -1 * value;
        }
        catch(Exception e) {
            throw new RuntimeException();
        }
    }
}
